#include "stdafx.h"

#include <gl/gl.h>
#include <gl/glu.h>

#include "camera.h"

//-------------------------------------------------------------
// For Camera class
//-------------------------------------------------------------
GCamera::GCamera(void)
{
}

GCamera::~GCamera()
{
}

void GCamera::projection() 
{
	//switch to projection
   	glMatrixMode(GL_PROJECTION);
   	glLoadIdentity();

	glRenderMode(GL_RENDER);

	//apply projective matrix
	double left		=  - m_width/2.0;
	double right	=  m_width/2.0;
	double bottom	=  - m_height/2.0;
	double top		=  m_height/2.0;

	glOrtho(left,right,bottom,top,m_near,m_far);

	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	gluLookAt(m_eye[0],m_eye[1],m_eye[2],m_ref[0],m_ref[1],m_ref[2],m_vecUp[0], m_vecUp[1], m_vecUp[2]);	
}

void GCamera::init()
{
	m_eye[0] = 0;
	m_eye[1] = 0;
	m_eye[2] = 100.0;

	m_ref[0] = 0.0;
	m_ref[1] = 0.0;
	m_ref[2] = 0.0;

	m_far = 600;
	m_near= 1;

	m_width = 4.0;
	m_height = 40.0;

	m_vecUp[0] = 0.0;
	m_vecUp[1] = 1.0;
	m_vecUp[2] = 0.0;

	m_screen[0] = 400;
	m_screen[1] = 400;
}

void GCamera::set_screen( int x, int y) 
{ 
	glViewport(0,0,x,y);
	if(y==0) y=1;
	double ratio = (double)x/(double)y;
	m_width *= (double)x/m_screen[0];
	m_height *= (double)y/m_screen[1];
	m_width =  m_height*ratio;
	m_screen[0] = x;
	m_screen[1] = y; 
}

void GCamera::set_eye(double eye_x,double eye_y,double eye_z)
{
	m_eye[0]=eye_x;
	m_eye[1]= eye_y;
	m_eye[2]= eye_z;
}

void GCamera::set_ref(double ref_x,double ref_y,double ref_z)
{
	m_ref[0] = ref_x;
	m_ref[1] = ref_y;
	m_ref[2] = ref_z;
}

void GCamera::set_vecUp(double up_dx,double up_dy,double up_dz)
{
	m_vecUp[0] = up_dx;
	m_vecUp[1] = up_dy;
	m_vecUp[2] = up_dz;
}

void GCamera::set_view_rect(double width,double height)
{
	m_width = width;
	m_height = height;
	double aspect = m_screen[0]/m_screen[1];
	m_width =  m_height*aspect;
}

void GCamera::get_view_rect(double& width,double& height)
{
	width = m_width;
	height = m_height;
}

void GCamera::Mousetorotate(double x, double y)
{
	
}
void GCamera::move_screen(int dx,int dy)
{
	double dxvw = dx*m_width/m_screen[0];
	double dyvw = dy*m_height/m_screen[1];
	move_view_by_space(dxvw,dyvw);
}
void GCamera::move_view_by_space(double dx, double dy)
{
	m_eye[0]+=-dx;
	m_eye[1]+=-dy;
	m_ref[0]+=-dx;
	m_ref[1]+=-dy;
}